import React, {PropsWithChildren, useState} from 'react'
import EditEmployeeForm, {EditEmployeeInitialValue} from "../components/EditEmployeeForm";
import Taro, {useLoad} from "@tarojs/taro";
import {EmployeeDetailResponse, updateEmployee} from "../apis/EmployeeApi";
import MyNavigationBar from "../components/MyNavigationBar";

export interface EditEmployeeProps extends PropsWithChildren {

}

const EditEmployee: React.FC<EditEmployeeProps> = ({}) => {
    const [initialValue,setInitialValue] = useState<EditEmployeeInitialValue>()
    const [id,setId] = useState<number>()
    useLoad(params=>{
        const id = Number(params.id)
        setId(id)
        let detail = JSON.parse(params.data) as EmployeeDetailResponse;
        setInitialValue({
            name:detail.name,
            phoneNumber:detail.phoneNumber,
            roleIds:detail.roles.map(it=>it.id),
        })
    })
    return <>
        <MyNavigationBar>编辑人员</MyNavigationBar>
        {!!id && <EditEmployeeForm initialValue={initialValue} onConfirm={async values => {
            updateEmployee(id,values)
            Taro.navigateBack()
        }}/>}
    </>
}

export default EditEmployee


